<?php

namespace app\selladmin\model;

use think\Model;

/*
 * 项目
 */

class Item extends Model {
    
    //更新时间字段
    protected $updateTime = null;

    //项目图片最大数量
    const IMG_NUMBER = 5;

    //是否显示
    const IS_DEL_1 = 1; //是
    const IS_DEL_2 = 2; //否

    //项目类型
    const TYPE_0 = 0; //默认
    const TYPE_1 = 1; //特色项目 <== 店长推荐
    const TYPE_2 = 2; //新项目
    const TYPE_3 = 3; //会员特惠区
    const TYPE_4 = 4; //仓库
    const TYPE_5 = 5; //天天特价

    //是否显示
    const ITEM_STATUS_1 = 1; //是
    const ITEM_STATUS_2 = 2; //否

    //项目对应的类型 
    public function itemsimgclass() {
        return $this->belongsTo('itemsimgclass', 'item_type', 'id')->field('id,name');
    }

    //项目对应的店铺
    public function store() {
        return $this->belongsTo('store', 'store_id', 'id')->field('id,store_name');
    }

    //查询项目的信息
    public function ItemInfo($store_id, $item_name, $pagenum) {
        if ($item_name) {
            $itemwhere['item_name'] = ['like', "%$item_name%"];
            $itemquery['item_name'] = $item_name;
        }
        if ($store_id) {
            $itemwhere['store_id'] = $store_id;
            $itemquery['store_id'] = $store_id;
        }
        $itemwhere['is_del'] = 1;
        $itemquery['is_del'] = 1;
        $itemList = Item::with('itemsimgclass,store')
                ->where($itemwhere)
                ->field("id,item_name,type,item_type,item_price,item_prices,sell_count,visit_count,store_id")
                ->order("update_time desc")
                ->order("id desc")
                ->paginate($pagenum)
                ->appends($itemquery);
        return $itemList;
    }
    
    /*
     * 项目图片
     */
    public function itemimg()
    {
        return $this->hasMany('Itemimg', 'item_id', 'id')->field('id,item_id, img, img_thumb');
    }
    
    /*
     * 项目类型集合
     */
    public static function getTypeMaps()
    {
        $array = [
            self::TYPE_0,
            self::TYPE_1,
            self::TYPE_2,
            self::TYPE_3,
            self::TYPE_4,
            self::TYPE_5,
        ];

        return $array;
    }

}
